<template>
  <div class="workList">
    <div class="tableConten" v-if="orderType == 'experiment'">
      <!-- 试验工单的时候展示 -->
      <div class="itemTitle">拷贝作业表单:</div>
      <div class="tebleDiv">
        <el-button
          class="el-button el-button--primary el-button--mini"
          size="mini"
          v-if="!disabled"
          @click="() => (showWorkListDialog = true)"
        >选择</el-button>
      </div>
    </div>
    <div class="tableConten">
      <div class="itemTitle">
        <span class="redPoint">*</span>
        作业准备:
      </div>
      <div class="tebleDiv">
        <editTableNew
          :mustColumn="mustColumn"
          :tableSetting="workListDetails"
          @handleClickAdd="addWorkList"
          :hideBtn="disabled"
          ref="workListTable"
        >
          <template #itemLable="{ editFormInfo }">
            <el-form :model="itemLable" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <template v-for="(item,index) in editFormInfo.itemLable">
                <el-form-item :label="item.name+'('+item.unit+'):'" prop="name" :key="index">
                  <el-input v-model="item.value"></el-input>
                </el-form-item>
              </template>
            </el-form>
          </template>
        </editTableNew>
      </div>
    </div>
    <div class="tableConten">
      <div class="itemTitle">
        <span class="redPoint">*</span>
        作业过程:
      </div>
      <div class="tebleDiv">
        <editTableNew
          :mustColumn="mustColumn"
          :tableSetting="workProcessDetails"
          :hideBtn="disabled"
          ref="workProcessTable"
        ></editTableNew>
      </div>
    </div>
    <workInstruction
      ref="instruction"
      :disabled="disabled"
      :orderType="orderType"
      :formData="formData"
      :workBookList="workBookList"
    ></workInstruction>
    <dialogPublic
      :title="'选择标准'"
      :show.sync="showStandard"
      @doSubmit="selectStandard"
      @doClose="() => (showStandard = false)"
    >
      <standardList
        :standardType="orderType"
        :typeId="typeId"
        v-if="showStandard"
        ref="standardList"
      />
    </dialogPublic>
    <dialogPublic
      :title="'作业表单'"
      @doSubmit="selectWorkList"
      @doClose="() => (showWorkListDialog = false)"
      :show.sync="showWorkListDialog"
      appendTobody
      fullscreen
    >
      <workListPage ref="workList" showType="dialogQuery" />
    </dialogPublic>
  </div>
</template>
<script>
import {
  getDetailsApi //试验工单-获取详情
} from "@/api/orderManagement/experimentOrder";
import standardList from "@/views/standardManagement/standardList.vue"; /* 标准选择 */
import workListPage from "./index"; /* 作业表单 */
import workInstruction from "../publicModule/workInstructionYC"; /* 作业指导书 */

import instructor from "@/views/standardManagement/workInstructionLibrary/instructionRelatedPage"; /* 指导书 */
import attachFormNew from "@/components/attach/attachFormNew";
import { getAttachList } from "@/api/common";
export default {
  props: {
    currentDept: {
      type: Object,
      default: () => {
        return {};
      }
    },
    formData: {
      type: Object,
      default: () => {
        return {};
      }
    },
    //是否可编辑
    disabled: {
      type: Boolean,
      default: false
    },
    //工单类型
    orderType: {
      type: String,
      default: null
    },
    pageType: {
      type: String,
      default: ""
    }
  },
  components: { standardList, workListPage, workInstruction },
  watch: {
    currentDept: {
      handler(val) {
        this.otherInfo.parentCode = val.id;
      },
      deep: true
    },
    formData: {
      handler(val) {
        this.attachSetting.businessId = val.id;
        this.setTableData();
      },
      deep: true
    },
    disabled: {
      handler(val) {
        /* 其它工单进行权限控制的时候要去掉this.orderType == "maintain" */
        if (
          this.pageType != "add" &&
          ["maintain", "experiment"].includes(this.orderType)
        ) {
          this.setTableData();
        }
      }
    }
  },
  data() {
    return {
      showBookDialog: false /* 指导书弹框 */,
      showWorkListDialog: false /* 作业表单弹框 */,
      workBookList: [],
      orgMemberSelect: {
        seleteType: "device",
        radio: true,
        props: {
          label: "name",
          children: "children"
        },
        column: [
          {
            label: "设备名称",
            prop: "name"
          }
        ],
        chosedData: []
      },
      otherInfo: {},
      showStandard: false,
      currentLineDevice: {},
      currentRow: null,
      typeId: null,

      hideBtn: false,
      workListData: [],
      itemLable: {},
      rules: [],
      workProcessData: [],
      /* 作业表单-作业准备 */
      workListDetails: {
        title: "",
        select: false,
        addMethons: true,
        maxHeight: "100%",
        btnArr: ["add"], //"edit",
        hideBtn: false,
        buttonArr: [
          {
            name: "删除",
            isShow: (row, index) => {
              if (index < 2) {
                return false;
              } else {
                return true;
              }
            },
            methods: (row, index) => {
              this.deletRow(row, index, "workList");
            }
          }
        ],
        data: [
          {
            type: "安全用具",
            tools: "安全帽、工作服、工作鞋"
          },
          {
            type: "工器具",
            tools: "工具袋、手电筒、梯子、清洁用具"
          }
        ],
        column: [
          {
            label: "类别",
            prop: "type",
            type: "text", //disableText
            required: true,
            width: 180
          },
          {
            label: "名称",
            prop: "tools",
            type: "text",
            autosize: true,
            required: true
          }
        ]
      },
      /* 作业表单-作业过程 */
      workProcessDetails: {
        btnArr: ["add"], //"edit",
        data: [],
        select: false,
        maxHeight: "100%",
        hideBtn: false,
        column: [
          {
            label: "设备",
            textAlign: "center",
            type: "device",
            prop: "equipmentName",
            idsStr: "equipmentId",
            required: true,
            readonly: true,
            methods: (data, row) => {
              row.type = data[0].typeId;
              this.typeId = data[0].typeId;
            }
          },
          {
            label: "作业标准",
            textAlign: "center",
            type: "click",
            icon: "el-icon-s-order",
            prop: "standardName",
            required: true,
            readonly: true,
            methods: (row, index) => {
              if (row.type) {
                this.currentRow = row;
                this.showStandard = true;
              } else {
                this.$message.warning("请先选择设备");
              }
            }
          }
        ],
        buttonArr: [
          {
            name: "删除",
            isShow: (row, index) => {
              return true;
            },
            methods: (row, index) => {
              this.deletRow(row, index, "process");
            }
          }
        ]
      },
      mustColumn: [1, 2],
      //附件设置
      attachSetting: {
        businessId: "", //单据的ID
        businessType: "workList"
      }
    };
  },
  created() {
    this.init();
  },
  mounted() {},
  methods: {
    init() {
      if (this.orderType == "experiment") {
        //试验工单
        this.workListDetails.buttonArr = [
          {
            name: "删除",
            isShow: (row, index) => {
              return true;
            },
            methods: (row, index) => {
              this.deletRow(row, index, "workList");
            }
          }
        ];
        this.workListDetails.data = [
          {
            type: "安全用具",
            tools: "安全带5、安全绳5、防坠器4、安全帽5、口罩8、护目镜3",
            editable: true
          },
          {
            type: "工器具",
            tools:
              "绝缘电阻测试仪（兆欧表）、直流电阻测试仪、电源线盘（电源）、人字梯、工具箱、秒表、温度湿度计",
            editable: true
          }
        ];
      }
    },
    // changeTableData(data) {
    //   this.typeIds = data.map(item=>{
    //     return item.typeId;
    //   })
    // },
    setTableData() {
      let val = this.formData;
      let workListData = [];
      let workProcessData = [];
      if (this.orderType == "experiment") {
        /* 试验工单 */
        workListData = val.experimentReadyPoList || [];
        workProcessData = val.experimentEquipmentPoList || [];
      } else if (this.orderType == "maintain") {
        /* 正常维护单 */
        workListData = val.maintainReadyPoList || [];
        workProcessData = val.maintainEquipmentPoList || [];
      } else if (this.orderType == "repair") {
        /* 计划检修 */
        workListData = val.repairReadyPoList; //作业过程
        workProcessData = val.repairEquipmentPoList; //作业准备getBookListData
      }
      this.workBookList = val.orderInstructionBeanList || []; /* 作业指导书 */
      if (!this.disabled) {
        workListData.map((i, index) => {
          if (this.orderType == "experiment") {
            /* 试验工单 */
            i.editable = true;
          } else {
            if (index > 1) {
              i.editable = true;
            }
          }
        });
        workProcessData.map(i => {
          i.editable = true;
        });
      } else {
        workListData.map((i, index) => {
          i.editable = false;
        });
        workProcessData.map(i => {
          i.editable = false;
        });
      }
      this.$refs.workListTable.setTableData(workListData); //作业准备
      this.$refs.workProcessTable.setTableData(workProcessData); //作业过程
    },
    //新增作业表单
    addWorkList(data, hadData) {
      let rowIndex = this.workListData.rowIndex;
      data.rowIndex = hadData.length + 1;
      // data.partSource = "spare_part";
      data.action = "删除";
      hadData.push(data);
      this.$refs.workListTable.setTableData(hadData);
    },
    async getWorkListData() {
      let workListData = await this.$refs.workListTable.getTableData();
      workListData.map((i, index) => {
        delete i.action;
        i.sort = index;
      });
      return workListData;
    },
    async getworkProcessData() {
      let workProcessData = await this.$refs.workProcessTable.getTableData();
      workProcessData.map((i, index) => {
        delete i.isChange;
        delete i.type;
        i.sort = index;
      });
      return workProcessData;
    },
    getBookListData() {
      let data = this.$refs.instruction.getInstructionData();
      return data;
    },
    //选择设备
    selectWorkStandard() {},
    //选择标准
    selectStandard() {
      let data = this.$refs.standardList.getselectData();
      this.currentRow.standardName = data.name;
      this.currentRow.standardId = data.id;
      this.showStandard = false;
    },
    //选择作业表单
    selectWorkList() {
      let data = this.$refs.workList.getRadioData();
      this.getDetails(data.id);
      this.showWorkListDialog = false;
    },
    //拷贝作业表单-获取试验工单作业表单
    getDetails(id) {
      getDetailsApi(id).then(res => {
        let data = res.data;
        let { experimentReadyPoList, experimentEquipmentPoList } = data;
        experimentReadyPoList.map(i => {
          if ([undefined, "DRAFT"].includes(this.formData.status)) {
            i.editable = true;
          } else {
            i.editable = false;
          }
        });
        experimentEquipmentPoList.map(i => {
          if ([undefined, "DRAFT"].includes(this.formData.status)) {
            i.editable = true;
          } else {
            i.editable = false;
          }
        });
        this.$refs.workListTable.setTableData(experimentReadyPoList); //作业准备
        this.$refs.workProcessTable.setTableData(experimentEquipmentPoList); //作业过程
      });
    },

    //删除表格的数据
    async deletRow(row, num, type) {
      let listArr = [];
      //获取所有填写的执行信息，不进行校验
      let data = [];
      if (type == "workList") {
        data = await this.$refs.workListTable.getTableData(false);
      } else if (type == "process") {
        data = await this.$refs.workProcessTable.getTableData(false);
      }
      data.map((i, index) => {
        if (index != num) {
          i.editable = true;
          listArr.push(i);
        }
      });
      if (type == "workList") {
        this.$refs.workListTable.setTableData(listArr);
      } else if (type == "process") {
        this.$refs.workProcessTable.setTableData(listArr);
      }
      //设置表格数据
    }
  }
};
</script>
<style lang="less" >
.workList {
  .tableConten {
    display: flex;
    flex-flow: wrap nowrap;
    margin-top: 10px;
    .redPoint {
      color: red;
    }
    .itemTitle {
      width: 120px;
    }
    .tebleDiv {
      width: 100%;
    }
  }
  .bookContent {
    width: 400px;
    border: 1px solid #ebebeb;
    border-radius: 4px;
    padding: 10px;
    margin-top: 10px;
  }
  .line {
    height: 30px;
    width: 100%;
    border: 1px dashed #ebebeb;
    padding: 0px 10px;
    display: flex;
    flex-flow: wrap nowrap;
    justify-content: space-between;
  }
  .line + .line {
    margin-top: 10px;
  }
}
</style>